local hx__pingpan = fk.CreateSkill{
  name = "hx__pingpan",
}

local U = require "packages.utility.utility"

hx__pingpan:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__pingpan.name) and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    local cards, choice = U.askForCardByMultiPatterns(
      player,
      {
        { ".|.|.|hand|.|.|.", 1, 1, "hx__pingpan1" },
        { ".|.|.|equip|.|.|.", 1, 1, "hx__pingpan2" },
        { ".", 0, 0, "hx__pingpan3" }
      },
      self.name,
      true,
      "平叛：你可以选择一项执行",
      {
        discard_skill = true
      }
    )
    if choice == "" then return false end
    event:setCostData(self, {cards = cards,choice = choice})
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = event:getCostData(self).cards
    if #card > 0 then
      room:throwCard(card, hx__pingpan.name, player)
      if event:getCostData(self).choice == "hx__pingpan1" then
        room:setPlayerMark(player, "@hx__pingpan_get-turn", 1)
      elseif event:getCostData(self).choice == "hx__pingpan2" then
        room:setPlayerMark(player, "@hx__pingpan_slash-turn", 1)
      end
      player:drawCards(1, hx__pingpan.name)
    else
      room:setPlayerMark(player, "@hx__pingpan_lost-turn", 1)
      room:askToUseVirtualCard(player, {
        name = "duel",
        skill_name = hx__pingpan.name,
        prompt = "请选择一名角色，视为你对其使用【决斗】",
        cancelable = false,
        extra_data = {
            bypass_times = false,
            extraUse = true,
        },
      })
    end
  end,
})

hx__pingpan:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      local n = 0
      if player:getMark("@hx__pingpan_get-turn") > 0 then
        n = n + 1
      end
      if player:getMark("@hx__pingpan_lost-turn") > 0 then
        n = n - 1
      end
      return n
    end
  end,
})

hx__pingpan:addEffect(fk.PreCardUse, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@hx__pingpan_slash-turn") > 0 and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    data.additionalDamage = (data.additionalDamage or 0) + 1
  end,
})


return hx__pingpan